﻿@using FormBuilder.Models
@using Microsoft.Extensions.Options
@inject IOptions<FormBuilderOptions> options

<RadzenDataGrid TItem="LabelTranslation" Data=@Labels>
    <Columns>
        <RadzenDataGridColumn Title="Language" TItem="LabelTranslation">
            <Template Context="labelTranslation">
                <RadzenLabel>@TranslateLanguage(labelTranslation.Language)</RadzenLabel>
            </Template>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn Title="Translation" TItem="LabelTranslation">
            <Template Context="labelTranslation">
                <RadzenTextBox @bind-Value="@labelTranslation.Translation"></RadzenTextBox>
            </Template>
        </RadzenDataGridColumn>
    </Columns>
</RadzenDataGrid>

@code {
    [Parameter] public List<LabelTranslation> Labels { get; set; }

    protected override void OnParametersSet()
    {
        base.OnParametersSet();
        if(Labels != null)
        {
            var missingTranslations = options.Value.SupportedLanguages.Where(l => !Labels.Any(la => la.Language == l.Code)).Select(l => new LabelTranslation
            {
                Language = l.Code
            });
            Labels.AddRange(missingTranslations);
            StateHasChanged();
        }
    }

    string TranslateLanguage(string lng)
    {
        return options.Value.SupportedLanguages.Single(l => l.Code == lng).DisplayName;
    }
}
